//
//  cCAddContactViewController.m
//  cyChat
//
//  Created by 楼楚 on 15/12/9.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "cCAddContactViewController.h"
@interface cCAddContactViewController ()<UITextFieldDelegate>



@end
@implementation cCAddContactViewController

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//添加好友
//1获取好友账号
    NSString *user = textField.text;
    NSLog(@"%@",user);
//    判断是否添加自己
    if ([user isEqualToString:[cCUserInfo sharedcCUserInfo].user]) {
        [self showAlert:@"不能添加自己为好友"];
        return YES;
    }
    
    NSString *jidStr = [NSString stringWithFormat:@"%@@%@",user,domain];
    XMPPJID *friendJid = [XMPPJID jidWithString:jidStr];
//    判断好友是否已经存在
    if ([[cCXMPPTool sharedcCXMPPTool].rosterStorge userExistsWithJID:friendJid xmppStream:[cCXMPPTool sharedcCXMPPTool].xmppStream]) {
        [self showAlert:@"当前好友已经存在"];
        return YES;
    }
    
//2发送添加好友的请求
//    添加好友 xmpp订阅
   
    [[cCXMPPTool sharedcCXMPPTool].roster subscribePresenceToUser:friendJid];
    return YES;
}

-(void)showAlert:(NSString *)msg{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:@"谢谢" otherButtonTitles:nil, nil];
    [alert show];
}

@end
